package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import services.UpdateDomain;

import models.DomainModel;
import models.IpAddressModel;

import data.DomainData;
import data.GoogleAdsenceData;
import data.GoogleAnalyticData;
import data.IpAddressData;
import data.OutLinkData;
import data.RecentSiteData;
import debugs.DomainDebug;
import debugs.IpAddressDebug;
import forms.CaptchaForm;

public class ProcessDomainServlet extends HttpServlet {
	/*public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setCharacterEncoding("UTF-8");
		TreeMap check = new TreeMap();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String domainName = URLDecoder.decode(path.toLowerCase().replaceAll("/process/", ""),"UTF-8");
		String type = (String) request.getParameter("type");
		String update = (String) request.getParameter("u");
		
		if (domainName == null || domainName.isEmpty() || !UrlHelper.checkDomain(domainName)) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		DomainData domainData = new DomainData();
		DomainModel domain = domainData.get(domainName);
		
		if (domain != null && update == null) {
			response.sendRedirect("/info/" + domainName);
			return;
		}
		
		request.setAttribute("check", check);
		request.setAttribute("type", type);
		request.setAttribute("domainName", domainName);
		try {
			request.getRequestDispatcher("/pages/process_domain.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}*/
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setCharacterEncoding("UTF-8");
		TreeMap check = new TreeMap();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String domainName = URLDecoder.decode(path.toLowerCase().replaceAll("/process/", ""),"UTF-8");
		String type = (String) request.getParameter("type");
		String update = (String) request.getParameter("u");
		
		if (domainName == null || domainName.isEmpty() || !UrlHelper.checkDomain(domainName)) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		DomainData domainData = new DomainData();
		DomainModel domain = domainData.get(domainName);
		
		if (domain != null && update == null) {
			response.sendRedirect("/info/" + domainName);
			return;
		} else {
			domain = new DomainModel();
		}
		
		CaptchaForm captcha = new CaptchaForm();
		check = captcha.checkValid(request, check);
		
		//if (check.size() == 0) {
			domain.setDomain(domainName);
			UpdateDomain updateDomain = new UpdateDomain(domain);
			domain = updateDomain.getDomain();
			
			check = updateDomain.getCheck();
			
			if (check.get("fDomain") != null || check.get("server") != null || check.get("siteinfo") != null) {
				
			} else {
				domainData.insert(domain);
				
				if (check.get("googleService") == null && !domain.getGoogleAnalyticId().isEmpty()) {
					GoogleAnalyticData analyticData = new GoogleAnalyticData();
					analyticData.add(domain.getGoogleAnalyticId(), updateDomain.getGoogleAnalyticSites());
				}
				
				if (check.get("googleService") == null && !domain.getGoogleAdsenseId().isEmpty()) {
					GoogleAdsenceData adsenceData = new GoogleAdsenceData();
					adsenceData.add(domain.getGoogleAdsenseId(), updateDomain.getGoogleAdsenceSites());
				}
				
				if (check.get("googleService") == null && !domain.getIpAddress().isEmpty()) {
					IpAddressData iData = new IpAddressData();
					IpAddressModel ipAddress = iData.add(domain.getIpAddress(), updateDomain.getSiteSameIp());
					iData.insert(ipAddress);
				}
				
				if (check.get("inlink") == null && !domain.getInlink().isEmpty()) {
					String[] inlinks = domain.getInlink().split(",");
					List<String> dInlinks = new ArrayList<String>();
					for (int i = 0; i < inlinks.length; i++) {
						dInlinks.add(inlinks[i]);
					}
					
					OutLinkData oData = new OutLinkData();
					oData.add(domain.getDomain(), dInlinks);
				}
				
				//Set into recentSiteUpdate
				RecentSiteData recentData = new RecentSiteData();
				recentData.setRecentUpdate(domain);
				
			}
		//}
		
		request.setAttribute("domain", domain);
		request.setAttribute("check", check);
		request.setAttribute("type", type);
		try {
			request.getRequestDispatcher("/pages/process_result.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
}
